/***************************************************************************//**
 * @file CGI/AirportApron.cpp
 * @author  Marek M. Cel <marekcel@mscsim.org>
 *
 * @section LICENSE
 *
 * Copyright (C) 2014 Marek M. Cel
 *
 * This file is part of MScSim. You can redistribute and modify it under
 * the terms of GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Further information about the GNU General Public License can also be found
 * on the world wide web at http://www.gnu.org.
 ******************************************************************************/
#ifndef AIRPORT_APRON_CPP
#define AIRPORT_APRON_CPP
#endif

////////////////////////////////////////////////////////////////////////////////

#include <iostream>

#include <osg/Geode>
#include <osg/Geometry>
#include <osg/Material>
#include <osg/ShapeDrawable>
#include <osg/Texture2D>

#include <osgDB/ReadFile>

#include <CGI/Airport.h>
#include <CGI/AirportApron.h>

////////////////////////////////////////////////////////////////////////////////

using namespace std;

////////////////////////////////////////////////////////////////////////////////

AirportApron::AirportApron( CGI_Data *data ) :
    m_data ( data )
{
    m_airportApronGroup = new osg::Group();
    m_airportApronGroup->setName( "AirportApronGroup" );

    createApron();
}

////////////////////////////////////////////////////////////////////////////////

void AirportApron::createApron()
{
    double x_from = 0.0;
    double y_cntr = 0.0;
    double y_edge = 0.0;
    double y_unto = 0.0;

    y_cntr = Airport::m_taxiDistance + 2.0 * Airport::m_halfWidth;

    y_unto = y_cntr + Airport::m_apronWidth + 1.0e-4;

    int iter_ul = 0;
    int iter_lr = 2;

    while ( y_edge < y_unto )
    {
        y_cntr = y_cntr + 2.0 * Airport::m_halfWidth;
        y_edge = y_cntr + Airport::m_halfWidth;

        // direction
        Airport::createSegment( m_airportApronGroup,
                                    -750.0, y_cntr - Airport::m_halfWidth, -2000.0, y_cntr + Airport::m_halfWidth,
                                   "data/textures/terrain/ground_1.png", true );

        x_from = -750.0;

        while ( fabs( x_from ) > 1.0e-4 )
        {
            double xx[ 4 ];
            double yy[ 4 ];

            xx[ 0 ] = x_from + 50.0;
            yy[ 0 ] = y_cntr - Airport::m_halfWidth;
            xx[ 1 ] = x_from + 50.0;
            yy[ 1 ] = y_cntr + Airport::m_halfWidth;
            xx[ 2 ] = x_from;
            yy[ 2 ] = y_cntr + Airport::m_halfWidth;
            xx[ 3 ] = x_from;
            yy[ 3 ] = y_cntr - Airport::m_halfWidth;

            Airport::createSegment( m_airportApronGroup,
                                        xx[iter_ul], yy[iter_ul], xx[iter_lr], yy[iter_lr],
                                        "data/textures/runway/apron_none.png" );

            iter_ul++;
            iter_lr++;

            if ( iter_ul > 3 ) iter_ul = 0;
            if ( iter_lr > 3 ) iter_lr = 0;

            x_from += 50.0;
        }

        // direction
        Airport::createSegment( m_airportApronGroup,
                                    750.0, y_cntr + Airport::m_halfWidth, 2000.0, y_cntr - Airport::m_halfWidth,
                                    "data/textures/terrain/ground_1.png", true );

        x_from = 750.0;

        while ( fabs( x_from ) > 1.0e-4 )
        {
            double xx[ 4 ];
            double yy[ 4 ];

            xx[ 0 ] = x_from - 50.0;
            yy[ 0 ] = y_cntr + Airport::m_halfWidth;
            xx[ 1 ] = x_from - 50.0;
            yy[ 1 ] = y_cntr - Airport::m_halfWidth;
            xx[ 2 ] = x_from;
            yy[ 2 ] = y_cntr - Airport::m_halfWidth;
            xx[ 3 ] = x_from;
            yy[ 3 ] = y_cntr + Airport::m_halfWidth;

            Airport::createSegment( m_airportApronGroup,
                                        xx[iter_ul], yy[iter_ul], xx[iter_lr], yy[iter_lr],
                                        "data/textures/runway/apron_none.png" );

            iter_ul++;
            iter_lr++;

            if ( iter_ul > 3 ) iter_ul = 0;
            if ( iter_lr > 3 ) iter_lr = 0;

            x_from -= 50.0;
        }
    }
}
